//
//  CatMessageCell.m
//  DoorBell
//
//  Created by Chun Hung Cheung on 2023/3/22.
//  Copyright © 2023 Chun Hung Cheung. All rights reserved.
//

#import "CatMessageCell.h"
@interface CatMessageCell()

@property(nonatomic,strong)UIView *dotview;
@property(nonatomic,strong)UILabel *timeLabel;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *dateLabel;
@property(nonatomic,strong)UIImageView *preview_image;
@property(nonatomic,strong)UIImageView *videoImageView;
@end
@implementation CatMessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (UIImageView*)preview_image
{
    if (!_preview_image) {
         _preview_image = [[UIImageView alloc]init];
        [self.contentView addSubview:_preview_image];
        [_preview_image mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView.mas_right).offset(-28);
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(45);
        }];
   }
    return _preview_image;
}



- (UILabel*)timeLabel
{
    if (!_timeLabel) {
         _timeLabel = [[UILabel alloc]init];
        [self.contentView addSubview:_timeLabel];
         _timeLabel.textColor = [HexChangeRGB colorWithHexString:@"#666666"];
         _timeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14.0];
         _timeLabel.numberOfLines = 0;
         _timeLabel.textAlignment = NSTextAlignmentLeft;
        [_timeLabel sizeToFit];
        [_timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(28);
            make.centerY.equalTo(self.contentView.mas_centerY);
        }];
   }
    return _timeLabel;
}

- (UILabel*)dateLabel
{
    if (!_dateLabel) {
         _dateLabel = [[UILabel alloc]init];
        [self.contentView addSubview:_dateLabel];
         _dateLabel.textColor = [HexChangeRGB colorWithHexString:@"#333333"];
         _dateLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16.0];
         _dateLabel.numberOfLines = 0;
         _dateLabel.textAlignment = NSTextAlignmentLeft;
        [_dateLabel sizeToFit];
        [_dateLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(28);
            make.centerY.equalTo(self.contentView.mas_centerY);
        }];
   }
    return _dateLabel;
}




- (UILabel*)titleLabel
{
     if (!_titleLabel) {
         _titleLabel = [[UILabel alloc]init];
        [self.contentView addSubview:_titleLabel];
         _titleLabel.textColor = [HexChangeRGB colorWithHexString:@"#333333"];
         _titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16.0];
         _titleLabel.numberOfLines = 0;
         _titleLabel.textAlignment = NSTextAlignmentLeft;
        [_titleLabel sizeToFit];
        [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(104);
            make.centerY.equalTo(self.contentView.mas_centerY);
        }];
   }
    return _titleLabel;
}

- (UIView*)dotview
{
      if(!_dotview){
         _dotview = [[UIView alloc]init];
         _dotview.layer.cornerRadius = 4;
         _dotview.layer.borderColor = [HexChangeRGB colorWithHexString:@"#E1E1E1"].CGColor;
         _dotview.layer.masksToBounds = YES;
        [self.contentView addSubview:_dotview];
        [_dotview mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(80);
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.width.height.mas_equalTo(8);
        }];
    }
    return _dotview;
}
- (UIView*)line1
{
      if(!_line1){
         _line1 = [[UIView alloc]init];
        [self.contentView addSubview:_line1];
        [_line1 mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.dotview.mas_top);
            make.top.equalTo(self.contentView.mas_top);
            make.width.mas_equalTo(1);
            make.centerX.equalTo(self.dotview.mas_centerX);
        }];
    }
    return _line1;
}

- (UIView*)line2
{
    if(!_line2){
        _line2 = [[UIView alloc]init];
        [self.contentView addSubview:_line2];
        [_line2 mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.dotview.mas_bottom);
            make.bottom.equalTo(self.contentView.mas_bottom);
            make.width.mas_equalTo(1);
            make.centerX.equalTo(self.dotview.mas_centerX);
        }];
    }
    return _line2;
}



- (UIImageView*)videoImageView
{
    if (!_videoImageView) {
         _videoImageView = [[UIImageView alloc]init];
        [self.preview_image addSubview:_videoImageView];
        [_videoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.preview_image.mas_centerY);
            make.centerX.equalTo(self.preview_image.mas_centerX);
            make.height.mas_equalTo(18);
            make.width.mas_equalTo(18);
        }];
    }
    return _videoImageView;
}




- (void)setModel:(CatMessageModel *)model
{
    _model = model;
    if(_model.isheader){
        
        if (_timeLabel) {
            [_timeLabel removeFromSuperview];
            _timeLabel = nil;
        }
        
        if (_titleLabel) {
            [_titleLabel removeFromSuperview];
            _titleLabel = nil;
        }
        if (_dotview) {
            [_dotview removeFromSuperview];
            _dotview = nil;
        }
         if (_line1) {
            [_line1 removeFromSuperview];
             _line1 = nil;
        }
        if (_line2) {
            [_line2 removeFromSuperview];
            _line2 = nil;
        }
         if([[Tooles timeToStrTime:_model.time] longLongValue] == [[Tooles timeToStrTime:[Tooles dateToUnixTime:[NSDate date]]] longLongValue]){
             self.dateLabel.text = NSLocalizedString(@"今天", nil);
         }else{
             NSArray *Array =  [[Tooles Date2StrD:_model.time] componentsSeparatedByString:@" "];
             NSString *str = [Array firstObject];
             NSArray *Array2 = [str componentsSeparatedByString:@"-"];
             self.dateLabel.text = [NSString stringWithFormat:@"%@月%@日",Array2[1],[Array2 lastObject]];
         }
        
        if (_videoImageView) {
            [_videoImageView removeFromSuperview];
            _videoImageView = nil;
        }
        
        if (_preview_image) {
            [_preview_image removeFromSuperview];
            _preview_image = nil;
        }
        
    }else{
        
        if (_dateLabel) {
           [_dateLabel removeFromSuperview];
            _dateLabel = nil;
        }
        
        
        NSArray *Array =  [[Tooles Date2StrD:_model.time] componentsSeparatedByString:@" "];
        NSString *str = [Array lastObject];
        NSArray *Array2 = [str componentsSeparatedByString:@":"];
        self.timeLabel.text = [NSString stringWithFormat:@"%@:%@",[Array2 firstObject],Array2[1]];
        if (self.model.message_type) {
            NSString *title;
            title = [NSString stringWithFormat:@"投喂了%@份粮食",_model.title];
            self.titleLabel.text = title;
            
        }else{
            self.titleLabel.text = _model.title;
        }
        self.dotview.backgroundColor = [HexChangeRGB colorWithHexString:@"#E0E0E0"];
        self.line1.backgroundColor = [HexChangeRGB colorWithHexString:@"#E6E6E6"];
        self.line2.backgroundColor = [HexChangeRGB colorWithHexString:@"#E6E6E6"];
        
        if (_model.previewFileId.length > 0) {
            
            NSURL *url = [YKBusinessFramework equesGetAlarmfileUrl:_model.bid fid:_model.previewFileId context:nil];
            
            
            
            [self.preview_image sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"logo_160_120"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                
                CGAffineTransform transform = CGAffineTransformIdentity;
                if (self.camera_id > 0 && self.camera_id != 4) {
                    if (self.camera_id == 3) {
                        transform = CGAffineTransformRotate(transform, M_PI_2);
                    }else if (self.camera_id == 5) {
                        
                    }
                    else{
                        transform = CGAffineTransformRotate(transform, -M_PI_2);
                    }
                }else{
                    if (self.camera_id == 4) {
                        transform = CGAffineTransformRotate(transform, M_PI);
                    }else{
                        transform = CGAffineTransformRotate(transform, M_PI_2);
                    }
                }
            
                self.preview_image.transform = transform;
                
                
                if (model.category == 2) {
                    
                    self.videoImageView.image  = [UIImage imageNamed:@"main_locck_v3_play"];
                    
                    CGAffineTransform transform = CGAffineTransformIdentity;
                    if (self.camera_id > 0 && self.camera_id != 4) {
                        if (self.camera_id == 3) {
                            transform = CGAffineTransformRotate(transform, -M_PI_2);
                        }else if (self.camera_id == 5) {
                        }else{
                            transform = CGAffineTransformRotate(transform, M_PI_2);
                        }
                    }else{
                        if (self.camera_id == 4) {
                            transform = CGAffineTransformRotate(transform, -M_PI);
                        }else{
                            transform = CGAffineTransformRotate(transform, -M_PI_2);
                        }
                    }
                    self.videoImageView.transform = transform;
                    
                }else{
            
                    if (_videoImageView) {
                        [_videoImageView removeFromSuperview];
                        _videoImageView = nil;
                    }
                }
            }];
    
        }else{
            
            if (_videoImageView) {
                [_videoImageView removeFromSuperview];
                _videoImageView = nil;
            }
            
            if (_preview_image) {
                [_preview_image removeFromSuperview];
                _preview_image = nil;
            }
            
            
        }
        
    }
}

@end
